<?php
/**
 * <i>@case</i>组件
 * 
 * 可以使用的参数有：
 * - x-value scalar 变量值
 * - x-default boolean 是否为默认
 * - x-break boolean 是否中断，默认为true
 * 
 * 示例：
 *  模板：
 * <code>
 * <span com="@switch" x-var="$integer" >
 * 	<span com="@case" x-value="1">1</span>
 * 	<span com="@case" x-value="2">2</span>
 * 	<span com="@case" x-value="3">3</span>
 *  <span com="@case">4</span>
 * </span>
 * </code>
 *
 * @version $Id: IMintCaseComponent.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.component
 * @since 0.0.3
 */
class IMintCaseComponent extends IAbstractMintComponent {	
	/**
	 * 取得支持的参数列表
	 *
	 * @return array
	 */	
	public function getSupportedParameters() {
		return array("x-value", "x-default", "x-break");
	}
	
	/**
	 * 取得HTML属性映射
	 *
	 * @return array
	 * @since 0.0.3
	 */		
	public function getHtmlAttrMapping() {
		return array();
	}	
	
	/**
	 * 检验组件定义
	 *
	 * @return boolean
	 * @since 0.0.3
	 */
	public function validate() {
		if ($this->getParentNode()->getAttribute(IMintConfig::$componentAttributeName) != "@switch") {
			$this->throwException("@case component must defined in @switch component");
			return false;
		}
		return true;
	}
	
	/**
	 * 处理组件
	 *
	 * @return boolean
	 */	
	public function processComponent() {
		$parameters = $this->getParameters();
		
		if (!isset($parameters["x-value"])) {
			$beginText = "<?php \n default: \n ?>";
			$endText = "<?php break; ?>";
		}
		else {
			$xValue = $parameters["x-value"];
			if (is_numeric($xValue)) {
				$beginText = "<?php \n case {$xValue}: \n ?>";
			}
			else {
				$xValue = "\"" . addslashes($xValue) . "\"";
				$beginText = "<?php \n case {$xValue}: \n ?>";
			}
			if (!array_key_exists("x-break", $parameters) || $parameters["x-break"] == "true") {
				$endText = "<?php break; ?>";
			}
			else {
				$endText = "";
			}
		}
		$this->addAboveScript($beginText);
		$this->addBelowScript($endText);
		return true;
	}
}

?>